// JavaScript Document
/*function makeSearchBox(){
	var searchInput=document.getElementById('searchBox').getElementsByTagName('input')[0];
	var searchInputTextDefault="search...";
	searchInput.value=searchInputTextDefault;
	searchInput.onfocus=function(){this.value=(this.value==searchInputTextDefault?'':this.value)};
	searchInput.onblur=function(){this.value=(this.value==''?searchInputTextDefault:this.value)};
}
function makePaging(){
	var strPaging='';
	var arrBlocks=document.getElementsByClassName("productShow");
	for(var i=0; i<arrBlocks.length; i++){
		strPaging+='<a href="#">'+(i+1)+'</a>';
	}
	document.write(strPaging);
}*/

/*function showPane(obj){
	obj.getElementsByClassName('infoPane')[0].style.display="block";
}
function hidePane(obj){
	obj.getElementsByClassName('infoPane')[0].style.display="none";
}*/
//var paneArrowClicked=false;
/*function togglePane(obj,i){
	if(i==1) showPane(obj);
	if(i==0) hidePane(obj);
}*/


jQuery('body').ready(function(){
	
	/*SCROLL TO TOP*/
	$('#wrapper').append('<div id="top">Back to Top</div>');
	$('#top').hide();
   	$(window).scroll(function() {
		if($(window).scrollTop() != 0) {
			$('#top').fadeIn();
		} else {
			$('#top').fadeOut();
		}
   	});
   	$('#top').click(function() {
		$('html, body').animate({scrollTop:0},500);
   	});
	/*-----------------*/

	/*LOGIN PANNEL*/
	doContextMenu('click',$('.loginban li:first'),$('.loginban .loginbox'));
	
	
	
	/* PAGING */
	$('.paging a').each(function () {
		if (location.href.indexOf(this.href) != -1) {
			$(this).removeClass();
			$(this).addClass('active');
		}
	});
	//$.scrollTo($('#paging'), {duration:'slow', easing:'easeOutCubic'});
	
	/*Slide gallery (home)*/
	//$('#frontpage-slider').aviaSlider();
	$('.aviaslider').aviaSlider();
	
	/*ACTIVE MENU ONCLICK*/
	$('#menu a').each(function () {
		if (location.href.indexOf(this.href) != -1) {
			$(this).attr('class', '');
			$(this).addClass('active');
		}
	});
	$('#menu a').each(function () {
		if (location.href.indexOf(this.href) != -1) {
			var ulTag = this;
			while (ulTag.TagName != "ul") {
				ulTag = ulTa.parent;
			}
			$(ulTag).brev.show();
		}
	});	
        
	/* Contact page */
	if(jQuery('#pageWrap').attr('class')=='contact'){
		txtTen='Tên';
		txtEmail='E-mail';
		txtDT='Điện thoại';
		txtDC='Địa chỉ';
		processFormText('#name',txtTen);
		processFormText('#email',txtEmail);
		processFormText('#phone',txtDT);
		processFormText('#address',txtDC);

		jQuery('.form-status').html('');
		jQuery('#contact-form form').submit(function(){
			//event.preventDefault();
			
			var name = jQuery("input#name").val();
			var email = jQuery("input#email").val();
			var phone = jQuery("input#phone").val();
			var address = jQuery("input#address").val();
			var message = jQuery("#message").val();
			jQuery('.form-status').html('');
			if (name == "" || name == txtTen) {
				jQuery('.form-status').html('Xin nhập tên của bạn !');
				jQuery("input#name").focus();
				return false;
			}
			if (emailCheck(email)==false) {
				jQuery('.form-status').html('Địa chỉ email không hợp lệ !');
				jQuery("input#email").val('');
				jQuery("input#email").focus();
				return false;
			}
			var messageCheckReturn=messageCheck(message);
			if (messageCheckReturn != true) {
				jQuery('.form-status').html(messageCheckReturn);
				jQuery("textarea#message").focus();
				return false;
			}
			if(phone==txtDT) jQuery("input#phone").val('');
			if(address==txtDC) jQuery("input#address").val('');
			jQuery('.form-status').html('<div class="loading">Sending...</div>');
			jQuery.ajax({
				type: "POST",
				url: jQuery('#contact-form form').attr('action'),
				data: jQuery('#contact-form form').serialize(),
				dataType: "text/plain",
				success: function(response) {
					jQuery('.form-status').html('');
					jQuery("#contact-form form").hide();
					response == "True" ? jQuery("#success").hide().fadeIn(1500) : jQuery("#error").hide().fadeIn(1500);
					jQuery('.shadow-sheet > .bg').each(function(){
						jQuery(this).css('height',jQuery(this).parent().outerHeight()+'px')
					});
				},
				error: function(response) {
					jQuery('.form-status').html('');
					jQuery("#contact-form form").hide();
					jQuery("#error").hide().fadeIn(1500);
					jQuery('.shadow-sheet > .bg').each(function(){
						jQuery(this).css('height',jQuery(this).parent().outerHeight()+'px')
					});
				}
			});
			return false;

		});
	}
	/* end Contact page */
	
	/* misc process*/
	jQuery('#left-col').css('height',jQuery('#container').outerHeight() + 'px');
	jQuery('#left-col .bg').css('height',jQuery('#container').outerHeight() + 'px');
	jQuery('#content .content-bg').css('height',jQuery('#container').outerHeight() + 'px');
	
	jQuery('.shadow-sheet > .shadow-top').each(function(){
		jQuery(this).css('width',jQuery(this).parent().outerWidth()+6+'px')
	});
	jQuery('.shadow-sheet > .shadow-bottom').each(function(){
		jQuery(this).css('width',jQuery(this).parent().outerWidth()+6+'px')
	});
	jQuery('.shadow-sheet > .bg').each(function(){
		jQuery(this).css('height',jQuery(this).parent().outerHeight()+'px')
	});
});

function processFormText(idFormText, defaultFormText){
	var searchFormText=jQuery(idFormText);
	searchFormText.val(defaultFormText);
	searchFormText.focus(function(){
		if(jQuery(this).val()==defaultFormText) jQuery(this).val('')
	});
	searchFormText.blur(function(){
		if(jQuery(this).val()=='') jQuery(this).val(defaultFormText)
	});
}
function emailCheck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false
	}
	if (str.indexOf(at,(lat+1))!=-1){
		return false
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false
	}
	if (str.indexOf(dot,(lat+2))==-1){
		return false
	}
	if (str.indexOf(" ")!=-1){
		return false
	}
	return true
}
function messageCheck(sMessage){
	var messageLimChar=10000;
	var countSpace=0;
	if(sMessage.length>messageLimChar) return 'Nội dung nhập quá lớn !<br> Hãy thu gọn nội dung của bạn.';
	for(var i=0; i<sMessage.length; i++){
		if(sMessage.charAt(i)==' ') countSpace++;
	}
	if(countSpace==sMessage.length || sMessage == "" || sMessage=='Message') return 'Xin nhập nội dung cần gửi !';
	return true;
}
function getTextNode(obj){
	var tempObj=obj.firstChild;
	while(tempObj.nodeType!=3) tempObj=tempObj.nextSibling;
	return tempObj;
}


// CONTEXT MENU ======================
function doContextMenu(eventType,jCtxTrigger,jCtxSheet){
	jCtxTrigger.bind('click',function(event){
		event.stopPropagation();
	})
	jCtxSheet.bind('click',function(event){
		event.stopPropagation();
	})
	$(document).bind('click',function(event){
		if(jCtxSheet.is(':visible'))
			jCtxSheet.slideUp('fast','easeOutCubic');
	})
	function toggleSheet(e){
		e.preventDefault();
		this.blur();
		jCtxSheet.slideToggle('fast','easeOutCubic');
	}
	jCtxTrigger.unbind(eventType+'.ctxMenu');
	jCtxTrigger.bind(eventType+'.ctxMenu',toggleSheet);
}


// COUNT DOWN TIMER ======================
function countdown(sYear,sMonth,sDay,sID){
	var austDay = new Date();
	austDay = new Date(sYear, sMonth, sDay);
	$(sID).countdown({until: austDay, format: 'HMS'});
}

